<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <todolist-form @getname="getname"></todolist-form>
      <todolist-list :arr="arr" @remove-item="removeItem"></todolist-list>
    </div>

    <script src="./lib/Vue3.js"></script>
    <script>
      const { createApp } = Vue;

      const TodolistForm = {
        emits: ["getname"],
        data() {
          return {
            text: "",
          };
        },
        methods: {
          add() {
            if (this.text) {
              this.$emit("getname", this.text);
              this.text = "";
            }
          },
        },
        template: `
          <div>
            <input type="text" v-model="text" />
            <button @click="add">添加</button>  
          </div>
        `,
      };

      const TodolistList = {
        // props: ["arr"],
        props: {
          arr: Array,
        },
        template: `
          <ul>
            <li v-for="item in arr" :key="item.id">{{ item.name }}
             - <button @click="remove(item.id)">删除</button></li>
          </ul>
        `,
        methods: {
          remove(id) {
            // console.log(id);
            this.$emit("removeItem", id);
          },
        },
      };

      createApp({
        data() {
          return {
            arr: [
              {
                id: 1,
                name: "zhangsan",
              },
              {
                id: 2,
                name: "lisi",
              },
            ],
          };
        },
        methods: {
          getname(text) {
            // console.log("getname", text);
            this.arr.push({ id: new Date().getTime(), name: text });
          },
          removeItem(id) {
            console.log("removeItem");
            this.arr = this.arr.filter((item) => item.id !== id);
          },
        },
        components: {
          TodolistForm,
          TodolistList,
        },
      }).mount("#app");
    </script>
  </body>
</html>
